package JUnitTests;

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import DataStorageClasses.*;
import Database.DatabaseConnector;

public class ClientConnectorTests 
{
	private static DatabaseConnector dbConnector;
	private static String firstName;
	private static String lastName;
	private static String businessName;
	private static String businessPassword;
	private static long id;
	
	@BeforeClass public static void initialize()
	{
		dbConnector = new DatabaseConnector();
		dbConnector.connect();
		
		businessName = "TesterBusiness";
		businessPassword = "password";
		dbConnector.addBusiness(businessName, businessPassword, null, null, null, null, null, null, null, null,null);
		
		firstName = "Tester";
		lastName = "Name";
		
		Client client = dbConnector.addClient(firstName, lastName, null, null, null, null, null, null, null, null, businessName);
		id = client.getClientId();
	}
	
	@AfterClass static public void destroy()
	{
		dbConnector.removeClient(id);
		dbConnector.removeBusiness(businessName, businessPassword);
		dbConnector.disconnect();
	}
	
	@Test public void testGetClient()
	{
		Client client = dbConnector.getClient(id,businessName);
		assertTrue(client.getClientId() == id);
		
		String testerBus = "tester";
		String testerPass = "pass";
		
		dbConnector.addBusiness(testerBus, testerPass, null, null, null, null, null, null, null, null, null);
		client = dbConnector.getClient(id, testerBus);
		assertTrue(client == null);
		dbConnector.removeBusiness(testerBus, testerPass);
	}
	
	@Test public void testAddClient()
	{
		Client client = dbConnector.addClient(null, null, null, null, null, null, null, null, null, null, null);
		assertTrue(client == null);
		
		client = dbConnector.addClient(null, null, null, null, null, null, null, null, null, null, businessName);
		assertTrue(client == null);
		
		client = dbConnector.addClient("name", null, null, null, null, null, null, null, null, null, "wrongbus");
		assertTrue(client == null);
	}
	
	@Test public void testRemoveClient()
	{
		boolean removed = dbConnector.removeClient(-3);
		assertFalse(removed);
	}
	
	@Test public void testEditClient()
	{
		String newFirst = "newname";
		String newLast = "newlast";
		String newTel1 = "newTel1";
		String newTel2 = "newTel2";
		String newCell = "newCell";
		String newStreet = "newStreet";
		String newCity = "new City";
		String newProvince = "new Province";
		String newEmail = "new Email";
		String newPost = "new Post";
		
		boolean updated = dbConnector.updateClient(id, newFirst, newLast, newEmail, newStreet, newCity, newProvince,
				newPost, newTel1, newTel2, newCell);
		assertTrue(updated);
		
		Client client = dbConnector.getClient(id, businessName);
		
		assertArrayEquals(newFirst.toCharArray(), client.getFirstName().toCharArray());
		assertArrayEquals(newLast.toCharArray(), client.getLastName().toCharArray());
		assertArrayEquals(newTel1.toCharArray(), client.getTel1().toCharArray());
		assertArrayEquals(newTel2.toCharArray(), client.getTel2().toCharArray());
		assertArrayEquals(newCell.toCharArray(), client.getCell().toCharArray());
		assertArrayEquals(newStreet.toCharArray(), client.getStreetAd().toCharArray());
		assertArrayEquals(newCity.toCharArray(), client.getCity().toCharArray());
		assertArrayEquals(newProvince.toCharArray(), client.getProvince().toCharArray());
		assertArrayEquals(newEmail.toCharArray(), client.getEmail().toCharArray());
		assertArrayEquals(newPost.toCharArray(), client.getPostalCode().toCharArray());
		
		updated = dbConnector.updateClient(-1, null, null, null, null, null, "jklh;", null, null, null, null);
		assertFalse(updated);
	}
	
	@Test public void testGetClients()
	{
		String newFirst = "newname";
		String newLast = "newlast";
		String newTel1 = "newTel1";
		String newTel2 = "newTel2";
		String newCell = "newCell";
		String newStreet = "newStreet";
		String newCity = "new City";
		String newProvince = "new Province";
		String newEmail = "new Email";
		String newPost = "new Post";
		
		dbConnector.updateClient(id, newFirst, newLast, newEmail, newStreet, newCity, newProvince,
				newPost, newTel1, newTel2, newCell);
		
		dbConnector.addClient(firstName, lastName, null, null, null, null, null, null, null, null, businessName);
		
		ArrayList<Client> clients = dbConnector.getClients(newFirst, newLast, newEmail, newStreet, newCity, 
				newProvince, newPost, newTel1, newTel2, newCell, businessName);
		
		assertNotNull(clients);
		assertTrue(clients.size() == 1);
		
		Client client = clients.get(0);
		
		assertArrayEquals(newFirst.toCharArray(), client.getFirstName().toCharArray());
		assertArrayEquals(newLast.toCharArray(), client.getLastName().toCharArray());
		assertArrayEquals(newTel1.toCharArray(), client.getTel1().toCharArray());
		assertArrayEquals(newTel2.toCharArray(), client.getTel2().toCharArray());
		assertArrayEquals(newCell.toCharArray(), client.getCell().toCharArray());
		assertArrayEquals(newStreet.toCharArray(), client.getStreetAd().toCharArray());
		assertArrayEquals(newCity.toCharArray(), client.getCity().toCharArray());
		assertArrayEquals(newProvince.toCharArray(), client.getProvince().toCharArray());
		assertArrayEquals(newEmail.toCharArray(), client.getEmail().toCharArray());
		assertArrayEquals(newPost.toCharArray(), client.getPostalCode().toCharArray());
		
		clients = dbConnector.getClients(newFirst, newLast, newEmail, newStreet, newCity, newProvince,
				newPost, newTel1, newTel2, newCell, "wrongbus");
		
		assertTrue(clients.isEmpty());
		
		clients = dbConnector.getClients(null, null, null, null, null, null,
				null, null, null, null, businessName);
		
		assertTrue(clients.size() == 2);
	}

}
